local Config = require("Config")
local Msg = require("core.Msg")
local MailLogicFW = require("mail.MailLogicFW")
local Human = require("core.Human")
local AdminLogic = require("admin.AdminLogic")
local bagW = require("bag.HandlerW")
local PayLogic = require("admin.PayLogicW")
local OpenServerActFW = require("activity.OpenServerActFW")
local OperateActFW = require("activity.OperateActFW")
local WildBossLogicW = require("wildboss.WildBossLogicW")
local Broadcast = require("broadcast.Broadcast")
local ChatReport = require("chat.ChatReport")
local MiddleChat = require("middle.MiddleChat")
local ChangeName = require("role.ChangeName")
local PfLogic = require("tencentApi.PfLogic")
--  world 线程中处理的GM指令
gm ={}


function gm.getInfo(human, uuid)
  local msg = {}
  msg.uuid = uuid
  bagW.CG_SEARCH_OTHER(human, msg)
end

function gm.mail(human, content)
	MailLogicFW.addSystemMessage(human._id, content, content)
end

function gm.savedb(human)
  Human.save(human)
  require("core.Timer").onSaveRank(true)
end

function gm.pay(human, val)
  local rmb = val + 0
  local yb = rmb *10
  local order_id = ""..os.time()
  order_id = order_id..math.random(1,10000)
  if PfLogic.isAgentTencent() then
    PayLogic.pay(human.account, order_id, rmb, yb, os.time())
  else
    local json = {account = human.account, order=order_id, amt = rmb, xianshiCnt = yb, ts = os.time()}
    AdminLogic.adminFunction.onLianyunPayCallback(json, true)
  end
end

function gm.blue(human, t)
  Human.sendBlueInfo(human)
end

function gm.test(human, t)
  Human.sendBlueInfo(human)
end

function gm.qpoint(human, t)
  if t == "1" then
    require("activity.operate.QPoint").getReward(human)
  elseif t == "2" then
    require("activity.operate.QPoint").checkEnough()
  elseif t== "88" then
    require("vip.VipLevel").buyMonthCard(human, 2)
  else
    require("activity.operate.QPoint").setReward()
  end
end

function gm.openday(human, day)
    OpenServerActFW.onDayChange(human, day)
    OperateActFW.onDayChange(human, day)
end

function gm.wildbossflush(human, time)
    WildBossLogicW.onFlushWildBoss(human, tonumber(time))
end

function gm.zhengdian(human, val)
    local v = val + 0
    require("core.Timer").onZhengDian(0,v)
end

function gm.showmem()
  local msg = Msg.wl.WL_ROBOT_TEST
  msg.type = 1
  for i = 0, 3 do
        Msg.send2logic_id(msg, i)
  end
  print("world-mem:"..math.floor(_G.collectgarbage("count")).."KB")
end

function gm.taskmarket(human, val)
  require("task.TaskMarket").onTaskMarketLogin(human, val)
  human.pf_info.appContractId = require("role.RoleFFI").ffi_string(human.db.appContractId)
end

function gm.middleopen(human, val)
  if val == "1" then
    require("middle.MiddleOpenDB").setMiddleOpen(true)
  elseif val == "0" then
    require("middle.MiddleOpenDB").setMiddleOpen()
  else
    if require("middle.MiddleOpenDB").db.isOpen == true then
      Broadcast.sendErr(human, "middle open")
    else
      Broadcast.sendErr(human, "middle not open")
    end
  end
end

function gm.middlestartact(human, val)
  require("middle.MiddleLogic").middleGM("middlestartact", val)
end

function gm.middlecloseact(human, val)
  require("middle.MiddleLogic").middleGM("middlecloseact", val)
end

function gm.middlefamilywar(human, val)
  require("middle.MiddleLogic").middleGM("middlefamilywar", val)
end
function gm.middleenter(human, val)
  require("middle.MiddleLogic").enter(human, "", val)
end
function gm.middleleave(human, val)
  require("middle.MiddleLogic").leave(human)
end

function gm.chatreport(human, val)
  ChatReport.chatReport(human,val)
end

function gm.search(human, val)
  require("bag.HandlerW").CG_SEARCH_OTHER(human, {uuid = human._id, type = 6})
end

function gm.changeName(human, val)
  ChangeName.changeName(human, val)
end
function LW_GM(human, msg)
	local cmd = gm[msg.cmd]
	if not cmd then
	   return
	end
	cmd(human, msg.arg)
end

function LW_MIDDLE_CHAT(human, msg)
  local bluelv = 0
  if human.db.blueVip ~=0 then
    bluelv = human.db.blueVipLv
  end 
  local blue = string.format("%d%d%d%d", bluelv, human.db.blueYearVip, human.db.blueSuperVip, human.db.level3366) 
	if Config.IS_MIDDLE == true then
		MiddleChat.syncChatFromMiddle(nil, human.db.svrIndex, msg.msg_type, msg.role_name, msg.msg, blue)
	else	  
		MiddleChat.sendChat2Middle(msg.msg_type, msg.role_name, msg.msg, blue)
	end
end

function LW_ROBOT_TEST(fd, msg)
  if msg.type == 1 then
      AdminLogic.lua_vm_info[msg.logicid] = msg.result
      print("logic-mem:"..msg.logicid.." "..msg.result)
  end
end

